﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SourceCodeFormatter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            // 打开文件对话框选择C#源文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "C# Files (*.cs)|*.cs|All Files (*.*)|*.*";
            openFileDialog.Title = "Select C# Source File";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;

                // 读取文件内容
                string[] lines = File.ReadAllLines(filePath);

                // 统计原始行数和单词数
                int originalLineCount = lines.Length;
                int originalWordCount = CountWords(lines);

                // 更新界面上的统计信息
                lblOriginalLineCount.Text = $"原始行数: {originalLineCount}";
                lblOriginalWordCount.Text = $"原始单词数: {originalWordCount}";

                // 删除空行和注释行后的处理
                var cleanedLines = RemoveEmptyLinesAndComments(lines);
                int cleanedLineCount = cleanedLines.Length;
                int cleanedWordCount = CountWords(cleanedLines);

                // 更新清理后的统计信息
                lblCleanedLineCount.Text = $"清理后行数: {cleanedLineCount}";
                lblCleanedWordCount.Text = $"清理后单词数: {cleanedWordCount}";

                // 统计单词出现次数
                var wordCounts = CountWordOccurrences(cleanedLines);

                // 清空现有的ListBox内容
                listBoxWordCount.Items.Clear();

                // 将每个单词的统计结果按出现次数降序显示
                foreach (var word in wordCounts.OrderByDescending(w => w.Value))
                {
                    listBoxWordCount.Items.Add($"{word.Key}: {word.Value}");
                }
            }
        }
            // 删除空行和注释行
            private string[] RemoveEmptyLinesAndComments(string[] lines)
            {
                return lines.Where(line =>
                    !string.IsNullOrWhiteSpace(line) &&     // 删除空行
                    !line.TrimStart().StartsWith("//")      // 删除注释行
                ).ToArray();
            }

            // 统计行中的单词数量
            private int CountWords(string[] lines)
            {
                int wordCount = 0;
                foreach (var line in lines)
                {
                    // 正则表达式匹配单词
                    wordCount += Regex.Matches(line, @"\b\w+\b").Count;
                }
                return wordCount;
            }

            // 统计单词出现次数
            private Dictionary<string, int> CountWordOccurrences(string[] lines)
            {
                var wordCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

                foreach (var line in lines)
                {
                    var words = Regex.Matches(line, @"\b\w+\b");
                    foreach (Match match in words)
                    {
                        string word = match.Value.ToLower();
                        if (wordCounts.ContainsKey(word))
                        {
                            wordCounts[word]++;
                        }
                        else
                        {
                            wordCounts[word] = 1;
                        }
                    }
                }

                return wordCounts;
            }
        }
    }

